package com.geek.water.controller;

/**
 * @author yangwenqi
 * @decs TODO
 * @date 2025/8/11
 */

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geek.water.constant.CodeConstant;
import com.geek.water.constant.MessageConstant;
import com.geek.water.entity.WorkOrder;
import com.geek.water.result.Result;
import com.geek.water.service.WorkOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 工单管理接口
 */
@RestController
@RequestMapping("/admin/workorder")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "后台系统 - 工单管理")
public class WorkOrderController {

    private final WorkOrderService workOrderService;

    @ApiOperation("创建工单")
    @PostMapping
    public Result createWorkOrder(@Valid @RequestBody WorkOrder workOrder) {
        try {
            boolean success = workOrderService.createWorkOrder(workOrder);
            if (success) {
                return new Result(CodeConstant.SUCCESS, MessageConstant.CREATE_WORKORDER_SUCCESS);
            }
            return new Result(CodeConstant.FAIL, MessageConstant.CREATE_WORKORDER_FAIL);
        } catch (Exception e) {
            log.error("创建工单失败", e);
            return new Result(CodeConstant.FAIL, "创建工单失败: " + e.getMessage());
        }
    }

    @ApiOperation("分页查询工单")
    @GetMapping("page")
    public Result page(
            @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "type", required = false) String type,
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "deviceId", required = false) Long deviceId,
            @RequestParam(value = "siteId", required = false) Long siteId,
            @RequestParam(value = "creatorId", required = false) Long creatorId,
            @RequestParam(value = "assigneeId", required = false) Long assigneeId) {
        try {
            Page<WorkOrder> page = workOrderService.findPage(currentPage, pageSize, type, status, deviceId, siteId, creatorId, assigneeId);
            if (page != null) {
                return new Result(CodeConstant.SUCCESS, MessageConstant.QUERY_WORKORDER_SUCCESS, page);
            }
            return new Result(CodeConstant.FAIL, MessageConstant.QUERY_WORKORDER_FAIL);
        } catch (Exception e) {
            log.error("查询工单列表失败", e);
            return new Result(CodeConstant.FAIL, "查询工单失败: " + e.getMessage());
        }
    }

    @ApiOperation("根据ID查询工单")
    @GetMapping("{id}")
    public Result findById(@PathVariable Long id) {
        try {
            WorkOrder workOrder = workOrderService.getById(id);
            if (workOrder != null) {
                return new Result(CodeConstant.SUCCESS, MessageConstant.QUERY_WORKORDER_SUCCESS, workOrder);
            }
            return new Result(CodeConstant.FAIL, MessageConstant.WORKORDER_NOT_EXIST);
        } catch (Exception e) {
            log.error("查询工单失败", e);
            return new Result(CodeConstant.FAIL, "查询工单失败: " + e.getMessage());
        }
    }



    @ApiOperation("派发工单")
    @PutMapping("/{id}/assign")
    public Result assignWorkOrder(@PathVariable Long id, @RequestParam Long assigneeId) {
        try {
            boolean success = workOrderService.assignWorkOrder(id, assigneeId);
            if (success) {
                return new Result(CodeConstant.SUCCESS, MessageConstant.ASSIGN_WORKORDER_SUCCESS);
            }
            return new Result(CodeConstant.FAIL, MessageConstant.ASSIGN_WORKORDER_FAIL);
        } catch (Exception e) {
            log.error("派发工单失败", e);
            return new Result(CodeConstant.FAIL, "派发工单失败: " + e.getMessage());
        }
    }

    @ApiOperation("更新工单状态")
    @PutMapping("/{id}/status")
    public Result updateStatus(@PathVariable Long id, @RequestParam String status) {
        try {
            boolean success = workOrderService.updateWorkOrderStatus(id, status);
            if (success) {
                return new Result(CodeConstant.SUCCESS, MessageConstant.UPDATE_WORKORDER_STATUS_SUCCESS);
            }
            return new Result(CodeConstant.FAIL, MessageConstant.UPDATE_WORKORDER_STATUS_FAIL);
        } catch (Exception e) {
            log.error("更新工单状态失败", e);
            return new Result(CodeConstant.FAIL, "更新工单状态失败: " + e.getMessage());
        }
    }

    @ApiOperation("完成工单")
    @PutMapping("/{id}/complete")
    public Result completeWorkOrder(@PathVariable Long id, @RequestParam String description) {
        try {
            boolean success = workOrderService.completeWorkOrder(id, description);
            if (success) {
                return new Result(CodeConstant.SUCCESS, MessageConstant.COMPLETE_WORKORDER_SUCCESS);
            }
            return new Result(CodeConstant.FAIL, MessageConstant.COMPLETE_WORKORDER_FAIL);
        } catch (Exception e) {
            log.error("完成工单失败", e);
            return new Result(CodeConstant.FAIL, "完成工单失败: " + e.getMessage());
        }
    }

    @ApiOperation("评价工单")
    @PutMapping("/{id}/evaluate")
    public Result evaluateWorkOrder(@PathVariable Long id, @RequestParam String evaluation) {
        try {
            boolean success = workOrderService.evaluateWorkOrder(id, evaluation);
            if (success) {
                return new Result(CodeConstant.SUCCESS, MessageConstant.EVALUATE_WORKORDER_SUCCESS);
            }
            return new Result(CodeConstant.FAIL, MessageConstant.EVALUATE_WORKORDER_FAIL);
        } catch (Exception e) {
            log.error("评价工单失败", e);
            return new Result(CodeConstant.FAIL, "评价工单失败: " + e.getMessage());
        }
    }

    @ApiOperation("根据设备ID查询工单")
    @GetMapping("device/{deviceId}")
    public Result findByDeviceId(@PathVariable Long deviceId) {
        try {
            List<WorkOrder> workOrders = workOrderService.findByDeviceId(deviceId);
            if (workOrders != null) {
                return new Result(CodeConstant.SUCCESS, MessageConstant.QUERY_WORKORDER_SUCCESS, workOrders);
            }
            return new Result(CodeConstant.FAIL, MessageConstant.QUERY_WORKORDER_FAIL);
        } catch (Exception e) {
            log.error("根据设备ID查询工单失败", e);
            return new Result(CodeConstant.FAIL, "查询工单失败: " + e.getMessage());
        }
    }

    @ApiOperation("根据站点ID查询工单")
    @GetMapping("site/{siteId}")
    public Result findBySiteId(@PathVariable Long siteId) {
        try {
            List<WorkOrder> workOrders = workOrderService.findBySiteId(siteId);
            if (workOrders != null) {
                return new Result(CodeConstant.SUCCESS, MessageConstant.QUERY_WORKORDER_SUCCESS, workOrders);
            }
            return new Result(CodeConstant.FAIL, MessageConstant.QUERY_WORKORDER_FAIL);
        } catch (Exception e) {
            log.error("根据站点ID查询工单失败", e);
            return new Result(CodeConstant.FAIL, "查询工单失败: " + e.getMessage());
        }
    }
}
